using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;

using System.Data;
using System.Data.SqlClient;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace PointBridge.SPDX
{
    //---------------------------------------------------------------------
    public class ValidationEx : Exception
    {
        public ValidationEx(string message, string varIdValue)
            : base(message)
        {
            _varId = varIdValue;
        }

        public string varId
        {
            get { return _varId; }
        }
        private string _varId;
    }

    public class ParseEx : Exception
    {
        public ParseEx(string message, string varIdValue)
            : base(message)
        {
            _varId = varIdValue;
        }
        public string varId
        {
            get { return _varId; }
        }
        private string _varId;
    }

    public class Parsing
    {
        //form parsing functions
        public static Dictionary<byte, bool> ParseMultiSelection(ListBox wListBox, string fieldName, bool strict, Dictionary<byte, bool> data)
        {
            foreach (ListItem item in wListBox.Items)
            {
                data[byte.Parse(item.Value)] = item.Selected;
            }
            return data;
        }

        public static string ParseSelectionLabelString(DropDownList wDropDown, string fieldName, bool strict)
        {
            if (wDropDown.SelectedItem.Text == "[None]") throw new ParseEx("Please select a valid option for " + fieldName, "");
            return wDropDown.SelectedItem.Text;
        }
        public static string ParseSelectionGetSPLooseLookupStrValue(DropDownList wDropDown, SPWeb web, string lookupListName, string lookupFieldName, string fieldName, bool strict)
        {
            int val = int.Parse(wDropDown.SelectedValue);
            //if (val == 0) throw new ParseEx("Please select a valid option for " + fieldName, "");
            string lookupVal = web.Lists[lookupListName].Items.GetItemById(val)[lookupFieldName].ToString();
            return lookupVal;
        }
        public static DateTime ParseSelectionGetSPLooseLookupDateValue(DropDownList wDropDown, SPWeb web, string lookupListName, string lookupFieldName, string fieldName, bool strict)
        {
            int val = int.Parse(wDropDown.SelectedValue);
            if (val == 0) throw new ParseEx("Please select a valid option for " + fieldName, "");
            DateTime lookupVal = (DateTime)web.Lists[lookupListName].Items.GetItemById(val)[lookupFieldName];
            return lookupVal;
        }
        public static int SPLookupGetIntID(string lookupValue)
        {
            string[] split = lookupValue.Split("#;".ToCharArray());
            if (split.Length != 3 || split[1] != "") throw new Exception("Lookup string ('" + lookupValue + "') is in invalid format");
            return int.Parse(split[0]);
        }
        public static string SPLookupGetStrValue(string lookupValue)
        {
            string[] split = lookupValue.Split("#;".ToCharArray());
            if (split.Length != 3 || split[1] != "") throw new Exception("Lookup string ('" + lookupValue + "') is in invalid format");
            return split[2];
        }
        public static int ParseSelection(DropDownList wDropDown, string fieldName, bool strict)
        {
            int val;
            try
            {
                val = int.Parse(wDropDown.SelectedValue);
            }
            catch
            {
                Web.MarkError(wDropDown);
                if (strict == true) throw new ParseEx(fieldName + " is invalid", wDropDown.ID);
                else val = 1;
            }
            return val;
        }
        public static int ParseSelection(string value, Type type, string fieldName, bool strict)
        {
            byte val;
            try
            {
                val = (byte)Enum.Parse(type, value);
            }
            catch
            {
                if (strict == true) throw new Exception(fieldName + " is invalid");
                else val = 1;
            }
            return val;
        }
        public static int ParseInt(TextBox wTextBox, string fieldName, bool strict)
        {
            int val;
            try
            {
                if ((strict == false) && (wTextBox.Text.Length == 0)) val = -1;
                else val = int.Parse(wTextBox.Text);
            }
            catch
            {
                Web.MarkError(wTextBox);
                throw new ParseEx(fieldName + " is invalid", wTextBox.ID);
            }
            return val;
        }
        public static int ParseInt(string value, string fieldName, bool strict)
        {
            int val;
            try
            {
                if ((strict == false) && (value.Length == 0)) val = -1;
                else val = int.Parse(value);
            }
            catch
            {
                throw new Exception(fieldName + " is invalid");
            }
            return val;
        }
        public static float ParseFlt(TextBox wTextBox, string fieldName, bool strict)
        {
            float val;
            try
            {
                if ((strict == false) && (wTextBox.Text.Length == 0)) val = -1;
                else val = (float)Math.Round(float.Parse(wTextBox.Text), 2);
            }
            catch
            {
                Web.MarkError(wTextBox);
                throw new ParseEx(fieldName + " is invalid", wTextBox.ID);
            }
            return val;
        }
        public static DateTime ParseDateMMYYYY(TextBox wTextBox, string fieldName, bool strict)
        {
            DateTime val = DateTime.MaxValue;
            try
            {

                if ((strict == false) && (wTextBox.Text.Trim().Length == 0))
                {
                    val = DateTime.MaxValue;
                }
                else
                {
                    if (Common.ValidateRx(wTextBox.Text, @"\d{1,2}/\d{4}") == true) val = DateTime.Parse(wTextBox.Text);
                    if (Common.ValidateRx(wTextBox.Text, @"\d{1,2}/\d{2}") == true) val = DateTime.Parse(wTextBox.Text.Replace("/", "/1/"));
                    if (Common.IsMaxDate(val) == true) throw new Exception("Invalid date format");
                }
            }
            catch
            {
                Web.MarkError(wTextBox);
                throw new ParseEx(fieldName + " is invalid", wTextBox.ID);
            }
            return val;
        }
        public static DateTime ParseDate(TextBox wTextBox, string fieldName, bool strict)
        {
            DateTime val;
            try
            {
                if ((strict == false) && (wTextBox.Text.Trim().Length == 0))
                {
                    val = DateTime.MaxValue;
                }
                else
                {
                    if (Common.ValidateRx(wTextBox.Text, @"\d{1,2}/\d{1,2}/\d{4}") != true) throw new Exception("Invalid date format");
                    val = DateTime.Parse(wTextBox.Text);
                }
            }
            catch
            {
                Web.MarkError(wTextBox);
                throw new ParseEx(fieldName + " is invalid", wTextBox.ID);
            }
            return val;
        }
        public static DateTime? ParseDateNull(DateTimeControl wDateTimeControl, string fieldName, bool strict)
        {
            if (wDateTimeControl.IsDateEmpty == true) return null;
            else return wDateTimeControl.SelectedDate;
        }
        public static string ParseStrNull(Label wLabel, string fieldName, bool strict)
        {
            string retVal = ParseStr(wLabel, fieldName, strict);
            if (retVal == "") retVal = null;
            return retVal;
        }
        public static string ParseStrNull(TextBox wTextBox, string fieldName, bool strict)
        {
            string retVal = ParseStr(wTextBox, fieldName, strict);
            if (retVal == "") retVal = null;
            return retVal;
        }
        public static string ParseStrNull(HiddenField wHiddenField, string fieldName, bool strict)
        {
            string retVal = ParseStr(wHiddenField, fieldName, strict);
            if (retVal == "") retVal = null;
            return retVal;
        }
        public static string ParseStr(Label wLabel, string fieldName, bool strict)
        {
            string val;
            try
            {
                val = wLabel.Text.Trim();
            }
            catch
            {
                throw new ParseEx(fieldName + " is invalid", wLabel.ID);
            }
            return val;
        }
        public static string ParseStr(TextBox wTextBox, string fieldName, bool strict)
        {
            string val;
            try
            {
                val = wTextBox.Text.Trim();
            }
            catch
            {
                Web.MarkError(wTextBox);
                throw new ParseEx(fieldName + " is invalid", wTextBox.ID);
            }
            return val;
        }
        public static string ParseStr(HiddenField wHiddenField, string fieldName, bool strict)
        {
            string val;
            try
            {
                val = wHiddenField.Value.Trim();
            }
            catch
            {
                //Web.MarkError(wHiddenField);
                throw new ParseEx(fieldName + " is invalid", wHiddenField.ID);
            }
            return val;
        }
        public static string ParseStr(string value, string fieldName, bool strict)
        {
            string val;
            try
            {
                val = value.Trim();
            }
            catch
            {
                //Web.MarkError(wHiddenField);
                throw new Exception(fieldName + " is invalid");
            }
            return val;
        }
        public static bool ParseBool(CheckBox wCheckBox, string fieldName, bool strict)
        {
            bool val;
            try
            {
                val = wCheckBox.Checked;
            }
            catch
            {
                Web.MarkError(wCheckBox);
                throw new ParseEx(fieldName + " is invalid", wCheckBox.ID);
            }
            return val;
        }
    }
}
